/*
    RoleCall provides a web service that web applications can use to find 
    the roles associated with a user that has been authenticated via OpenID. 
    Also provides a web-based user interface to manage the user roles 
    associated with partner web applications.
    
    Development of this software was supported in part by the David and Lucile 
    Packard Foundation and by the the Office Of Naval Research (ONR) 
     
    Copyright (c) 2012, 
    Monterey Bay Aquarium Research Institute - MBARI ( www.mbari.org )
    Michael Godin ( mikegodin AT users DOT sourceforge DOT net )

    RoleCall is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    RoleCall is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar (see the file COPYING.GPL).  If not, see 
    <http://www.gnu.org/licenses/>.  
*/

package org.mbari.rolecall.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * User coresponds to a user identifed by an e-mail and OpenID identity.
 * 
 * @author godin
 * 
 */
@SuppressWarnings("serial")
@Entity(name = User.TABLE_NAME)
public class User implements Serializable {

    // field names to simplify QueryBuilder
    public static final String FIELD_NAME_EMAIL = "email";

    public static final String FIELD_NAME_OPEN_ID = "openId";

    public static final String FIELD_NAME_ROLE_REQUEST = "roleRequest";

    public static final String TABLE_NAME = "User";

    @Column(name = FIELD_NAME_EMAIL, nullable = false)
    protected String email;

    @Id
    @GeneratedValue
    protected int id;

    protected String name;

    @Column(name = FIELD_NAME_OPEN_ID)
    protected String openId;

    protected String[] roleArray;

    @Column(name = FIELD_NAME_ROLE_REQUEST)
    protected String roleRequest;

    protected String roles;

    // No-arg constructor with at package visibility for persistence
    User() {
    }

    public User(String email, String openId) {
        this.email = email;
        this.openId = openId;
    }

    public User(String email, String name, String openId) {
        this.email = email;
        this.name = name;
        this.openId = openId;
    }

    /**
     * @return the first item in emails array
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the openId
     */
    public String getOpenId() {
        return openId;
    }

    /**
     * @return the roleRequest
     */
    public String getRoleRequest() {
        return roleRequest;
    }

    /**
     * @return the roles
     */
    public String getRoles() {
        return roles;
    }

    public boolean inRole(String aRole) {
        if (null != roleArray) {
            for (String role : roleArray) {
                if (role.equals(aRole)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param roleRequest
     *            the roleRequest to set
     */
    public void setRoleRequest(String roleRequest) {
        this.roleRequest = roleRequest;
    }

    /**
     * @param roles
     *            the roles to set
     */
    public void setRoles(String roles) {
        this.roles = roles;
        this.roleArray = null == roles ? null : roles.split("[\\s,;]+");
    }

}
